<?php
/**
 * Outputs data to user on request
 *
 * Somewhat like a template switch
 **/

require_once 'config.php';
require_once 'util/perm.php';

if( !is_string($_REQUEST['type']) ) {
  // error BREAK
}

switch( $_REQUEST['type'] ) {
  case 'POST':
          echoPosts(); break;
  case 'THREAD':
          echoThreads(); break;
  case 'FORA':
          echoFora(); break;
  default:
          echoFora(); break;
}

// again, abstraction beckons us
function echoPosts() {
  require_once 'db/db.php'; // for default_increment();
  require_once 'db/get/post.php';

  if( $_REQUEST['tnum'] < 0 ) {
    die( "BREAKAGE" ); // BREAKAGE
  }
  $tnum = (int)$_REQUEST['tnum'];
  $start = (int)$_REQUEST['start'];
  $inc = ((int)$_REQUEST['inc'] > 0) ? (int)$_REQUEST['inc'] : default_increment();
  
  $posts = getPosts( $tnum, $start, $inc );
  $count = postCount( $tnum );

  require_once 'tpl/use/ShowPosts.php';
  skin_ShowPosts( $tnum, $posts, $count, $start, $inc );

  $posts = NULL;
}
function echoThreads() {
  require_once 'db/get/thread.php';

  $inc = ((int)$_REQUEST['inc'] > 0) ? intval((int)$_REQUEST['inc']) : default_increment();
  if( $_REQUEST['fnum'] > 0 ) {
    $fnum = (int)intval($_REQUEST['fnum']);
    $start = (int)intval($_REQUEST['start']);
  } elseif( $_REQUEST['tnum'] > 0 ) {
    // aye' lass slightly slower, but what can ya do, now?
    $tid = (int)intval($_REQUEST['tnum']);

    // Yes, this doesn't work. :\ Maybe change to getFidFromTid( $tid ); ?
    //  Then just place on front page
    $thread = getThread( $tid );
    $fnum = $thread['fid'];
    $start = ($thread['tid'] > $inc) ? (int)(($thread['tid']-1)/$inc)*$inc : 0;
    $thread = NULL;
  } else {
    die( "BREAKAGE!" ); // BREAKAGE
  }

  $threads = getThreads( $fnum, $start, $inc );
  $count = threadCount($fnum);

  require_once 'tpl/use/ShowThreads.php';
  skin_ShowThreads( $fnum, $threads, $count, $start, $inc );

  $threads = NULL;

}
function echoFora() {
  require_once 'db/db.php'; // for default_increment();
  require_once 'db/get/forum.php';

  $start = (int)$_REQUEST['start'];
  $end = ((int)$_REQUEST['inc'] > 0) ? (int)$_REQUEST['inc'] : default_increment();

  $fora = getFora( null, $start, $end );

  require_once 'tpl/use/ShowFora.php';
  skin_ShowFora( $fora );
}

?>

